//
//  HelpViewModel.swift
//  Exchange
//
//  Created by rui on 2018/7/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class HelpCenterViewModel: BaseViewModel {
    
    var id: Int = 0
    
    var dataArray:[HelpCenterModel] = []
    
    private lazy var helpCenterApi: HelpCenterRequest = {
        let api = HelpCenterRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    func loadHelpList(id: Int) {
        self.id = id
        self.helpCenterApi.loadData()
    }
}

extension HelpCenterViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? HelpCenterRequest {
            var flag = false
            if let dt = data?["data"] as? [[String:Any]] {
                if let jsonData = dt.jsonData {
                    do{
                        let list = try JSONDecoder().decode(Array<HelpCenterModel>.self, from: jsonData)
                        self.dataArray.removeAll()
                        self.dataArray.append(contentsOf: list)
                        flag = true
                    }catch {
                        flag = false
                        dPrint(error)
                    }
                }
            }
            self.response?(ResponseState.mine_help_list,flag, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? HelpCenterRequest {
            self.response?(ResponseState.mine_help_list,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? HelpCenterRequest {
            pramas.updateValue(id, forKey: kID)
        }
        return pramas
    }
    
}
